package com.hxy.moobile.controller;

import com.hxy.travel.constant.MessageConstant;
import com.hxy.travel.constant.RedisMessageConstant;
import com.hxy.travel.utils.SMSUtilsHZ;
import com.hxy.travel.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;
import com.hxy.travel.entity.Result;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;

    @PostMapping("/send4Order.do")
    public Result send4Order( String telephone ){
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        try {
            SMSUtilsHZ.sendShortMessage(telephone,code.toString());
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(telephone+MessageConstant.SEND_VALIDATECODE_FAIL);

        }
        jedisPool.getResource().setex(telephone,300,code.toString());

        return Result.ok(MessageConstant.SEND_VALIDATECODE_SUCCESS);


    }


}
